home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 406_01 / disked25 / source / direct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-13  |  4.3 KB  |  215 lines

  1. /***
  2. *direct.c - functions for file I/O on directories
  3. *
  4. *Copyright (c) 1993-1994, Gregg Jennings.  All wrongs reserved.
  5. *   P O Box 200, Falmouth, MA 02541-0200
  6. *
  7. *Purpose:
  8. *   Beginning of Unix like directory I/O.
  9. *
  10. *Notice:
  11. *   This progam may be freely used and distributed.  Any distrubution
  12. *   with modifications must retain the above copyright statement and
  13. *   modifications noted.
  14. *   No pulp-publication, in whole or in part, permitted without
  15. *   permission (magazines or books).
  16. *******************************************************************************/
  17.  
  18. /*
  19.  
  20.    Version 1.0 14-Jan-1994
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <direct.h>
  26. #include <dos.h>
  27.  
  28. #include "diskio.h"
  29. #include "dirent.h"
  30. #include "direct.h"
  31.  
  32. /***
  33. *cwdstart() - get starting sector number of current directory
  34. *
  35. ****/
  36.  
  37. dword cwdstart(void)
  38. {
  39. int i;
  40. char cwd[67],tbuf[67];
  41.  
  42.    getcwd(cwd,67);            /* get current directory */
  43.    if (strlen(cwd) > 3)       /* i.e. not "C:\" */
  44.    {
  45.       char *p,*t;             /* temps */
  46.       chdir("..");            /* set to parrent directory */
  47.       strcpy(tbuf,cwd);       /* make modifiable copy */
  48.  
  49.       /* get last token (this directory name) */
  50.  
  51.       t = p = strtok(tbuf,"\\");
  52.       while (p && (p = strtok(NULL,"\\")) != NULL)
  53.          t = p;
  54.  
  55.       i = file_start(t);      /* get starting cluster of the dir */
  56.       chdir(cwd);             /* set back to right directory */
  57.  
  58.       return clustertosector((word)i); /* convert to sector */
  59.    }
  60.    return dir_sector;
  61. }
  62.  
  63. /***
  64. *filestart() - get starting sector number of file
  65. *
  66. ****/
  67.  
  68. int file_start(char *file)
  69. {
  70. struct DIR dir;
  71.  
  72.    if (direntry(file,&dir))
  73.       return dir.start;
  74.    else
  75.       return 0;
  76. }
  77.  
  78. /***
  79. *direntry() - get directory entry of file
  80. *
  81. ****/
  82.  
  83. int direntry(char *name, struct DIR *dir)
  84. {
  85. int i;
  86. struct xFCB fcb;                    /* extended FCB */
  87. struct xDIR _far *xdir;             /* temp extended DIR */
  88.  
  89.    memset(&fcb.name[0],' ',11);     /* pad with spaces */
  90.    fcb.ff = 0xff;                   /* extended FCB flag */
  91.    fcb.a = 0x3f;                    /* ATTRIB */
  92.    fcb.drive = 0;                   /* current drive */
  93.  
  94.    /* put in name */
  95.  
  96.    for (i = 0; *name != '.' && *name != '\0'; i++)
  97.       fcb.name[i] = *name++;
  98.    if (*name == '.')
  99.       for (name++, i = 0;*name != '\0'; i++)
  100.          fcb.ext[i] = *name++;
  101.  
  102.    /* force xDIR pointer to DTA */
  103.  
  104.    xdir = (struct xDIR _far *)_dos_getdta();
  105.  
  106.    /* make the call */
  107.  
  108.    if (_dos_xfcb_find(&fcb) == 0)
  109.    {
  110.       _fmemcpy((void _far *)dir,&xdir->name,sizeof(struct DIR));
  111.       return 1;
  112.    }
  113.    return 0;
  114. }
  115.  
  116. /***
  117. *get_volume() - get volume label
  118. *
  119. ****/
  120.  
  121. void get_volume(char *v)
  122. {
  123. struct find_t fi;
  124. char cwd[67];
  125.  
  126.    getcwd(cwd,67);               /* get current directory */
  127.    chdir("\\");                  /* set root directory */
  128.    if (_dos_findfirst("*.*",_A_VOLID,&fi) == 0)
  129.    {
  130.       int i;                     /* copy all but the '.' */
  131.       for (i = 0; i < 13; i++)
  132.          if (fi.name[i] != '.')
  133.             *v++ = fi.name[i];
  134.    }
  135.    chdir(cwd);
  136. }
  137.  
  138. /***
  139. *_dos_xfcb_find() - fills DOS DTA with Extended DIR, INT 21/11
  140. *
  141. *  ver   2.0 13-Jan-1994   save DS
  142. ****/
  143.  
  144. #ifdef _QC
  145.  
  146. int _dos_xfcb_find(struct xFCB *xfcb)
  147. {
  148. int r;
  149.    _asm  mov   dx, WORD PTR [xfcb]
  150. #ifdef __LARGE__
  151.    _asm  push  ds
  152.    _asm  mov   ds, WORD PTR [xfcb+2]
  153. #endif
  154.    _asm  mov   ah,0x11
  155.    _asm  int   0x21
  156. #ifdef __LARGE__
  157.    _asm  pop   ds
  158. #endif
  159.    _asm  sub   ah,ah
  160.    _asm  mov   r,ax
  161.    return r;
  162. }
  163.  
  164. #else
  165.  
  166. #pragma optimize("gle",off)
  167. #pragma warning(disable:4035)
  168.  
  169. int _dos_xfcb_find(struct xFCB *xfcb)
  170. {
  171.    _asm  mov   dx,WORD PTR [xfcb]
  172. #ifdef __LARGE__
  173.    _asm  push  ds
  174.    _asm  mov   ds,WORD PTR [xfcb+2]
  175. #endif
  176.    _asm  mov   ah,0x11
  177.    _asm  int   0x21
  178. #ifdef __LARGE__
  179.    _asm  pop   ds
  180. #endif
  181.    _asm  sub   ah,ah
  182. }
  183.  
  184. #endif
  185.  
  186. /***
  187. *_dos_getdta() - returns address of DOS DTA (far)
  188. *
  189. ****/
  190.  
  191. #ifdef _QC
  192.  
  193. void _far *_dos_getdta(void)
  194. {
  195. char _far *dta;
  196.  
  197.    _asm  mov   ah,0x2f
  198.    _asm  int   0x21
  199.    _asm  mov   WORD PTR [dta+2],es
  200.    _asm  mov   WORD PTR [dta],bx
  201.    return dta;
  202. }
  203.  
  204. #else
  205.  
  206. void _far *_dos_getdta(void)
  207. {
  208.    _asm  mov   ah,0x2f
  209.    _asm  int   0x21
  210.    _asm  mov   ax,bx
  211.    _asm  mov   dx,es
  212. }
  213.  
  214. #endif
  215.